package map;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
 Map的遍历
 遍历所有key
 遍历所有value
 遍历每一组键值对
 */
public class MapDemo2 {
    public static void main(String[] args) {
        Map<String,Integer> map = new HashMap<>();
        map.put("语文",80);
        map.put("数学",13);
        map.put("英语",12);
        map.put("物理",80);
        map.put("化学",90);
        System.out.println(map);
        Set<String> keySet = map.keySet();
        for(String key : keySet) {
            System.out.println("key:" + key);
        }
        Set<Map.Entry<String,Integer>> entrySet = map.entrySet();
        for(Map.Entry<String,Integer> e : entrySet){
            String key = e.getKey();
            Integer value = e.getValue();
            System.out.println(key+":"+value);
        }
        Collection<Integer> values = map.values();
        for(Integer value : values){
            System.out.println("value:"+value);
        }
        /*
        keySet.forEach(new Consumer<String>() {
            public void accept(String s) {
                System.out.println(s);
            }
        });

         */
        //keySet.forEach(s -> System.out.println(s));//lamba表达式的方法
        //keySet.forEach(System.out::println);//对象::方法  的简写方法
        /*
        map.forEach(new BiConsumer<String, Integer>() {
            public void accept(String k, Integer v) {
                System.out.println(k+":"+v);
            }
        });

         */
        map.forEach((k, v)-> System.out.println(k+":"+v));


    }
}
